import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.event.ComponentListener;
import java.awt.image.BufferedImage;

public class InterfaceSelector extends JPanel implements ActionListener{
		private MusicSelector musicSelector;
		private ContentSelector contentSelector;
		private MyDesktop desktop;
		private String currentSong = "Click play to start selected song.";
		private String currentArtist = "No song playing.";
		private String songFileName = "", currentAlbum = "";
		private JTextArea artist, song;
		private DecodeAndPlayAudio newPlayer;
		private CardLayout cardlayout=new CardLayout();
		private JPanel cards=new JPanel (cardlayout);
		private String select="music";
		private JButton getImage;
		private Boolean playing = false, paused = false;
		private ImageIcon img1 = new ImageIcon("pictures/pause.png");
        private ImageIcon rollover1 = new ImageIcon("pictures/pausehover.png");
        private JButton playButton;
        private ImageIcon img0 = new ImageIcon("pictures/play.png");
        private ImageIcon rollover0 = new ImageIcon("pictures/playhover.png");
        private JSlider slider;
        
        
        public InterfaceSelector(){
			setLayout((new BorderLayout()));
			setBackground(Color.WHITE);
			desktop=new MyDesktop();
			musicSelector = new MusicSelector();
			contentSelector=new ContentSelector(musicSelector);
			cards.add(desktop,"content");
			cards.add(musicSelector,"music");
			add(cards,BorderLayout.CENTER);
			
			JPanel contentPane = new JPanel() {
	        	@Override
	        	protected void paintComponent(Graphics grphcs) {
	        		Graphics2D g2d = (Graphics2D) grphcs;
	        		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
	        				RenderingHints.VALUE_ANTIALIAS_ON);

	        		GradientPaint gp = new GradientPaint(0, 0,
	        				Color.blue, 0, getHeight(),
	        				Color.black);

	        		g2d.setPaint(gp);
	        		g2d.fillRect(0, 0, getWidth(), getHeight());

	        		super.paintComponent(grphcs);
	        	}
	        };
	        
            playButton = new JButton(img0);
            playButton.setBorder(null);
            playButton.setBackground(null);
            playButton.setMinimumSize(new Dimension(60,60));
            playButton.setActionCommand("play");
            playButton.addActionListener(this);
            playButton.setRolloverEnabled(true);
            playButton.setRolloverIcon(rollover0);
            playButton.setOpaque(false);
            playButton.setContentAreaFilled(false);
            
            
            JButton pauseButton = new JButton(img1);
            pauseButton.setBorder(null);
            pauseButton.setMinimumSize(new Dimension(60 ,60));
            pauseButton.setActionCommand("pause");
            pauseButton.addActionListener(this);
            pauseButton.setRolloverEnabled(true);
            pauseButton.setRolloverIcon(rollover1);
            playButton.setOpaque(false);
            playButton.setContentAreaFilled(false);
            
            ImageIcon img2 = new ImageIcon("pictures/next.png");
            JButton nextButton = new JButton(img2);
            nextButton.setBorder(null);
            nextButton.setMinimumSize(new Dimension(60 ,60));
            nextButton.setActionCommand("stop");
            nextButton.addActionListener(this);
            nextButton.setRolloverEnabled(true);
            ImageIcon rollover2 = new ImageIcon("pictures/nexthover.png");
            nextButton.setRolloverIcon(rollover2);
            nextButton.setOpaque(false);
            nextButton.setContentAreaFilled(false);

            ImageIcon img3 = new ImageIcon("pictures/previous.png");
            JButton previousButton = new JButton(img3);
            previousButton.setBorder(null);
            previousButton.setMinimumSize(new Dimension(60 ,60));
            previousButton.setActionCommand("stop");
            previousButton.addActionListener(this);
            previousButton.setRolloverEnabled(true);
            ImageIcon rollover3 = new ImageIcon("pictures/previoushover.png");
            previousButton.setRolloverIcon(rollover3);
            previousButton.setOpaque(false);
            previousButton.setContentAreaFilled(false);
            
            JButton libraryButton = new JButton("Library");
            libraryButton.setMinimumSize(new Dimension(100 ,40));
            libraryButton.setActionCommand("library");
            libraryButton.addActionListener(this);
            
            UIDefaults sliderDefaults = new UIDefaults();


           slider = new JSlider(0, 100, 50);
            final JLabel sliderLabel = new JLabel("Volume: 50%");
            sliderLabel.setForeground(Color.white);
            slider.addChangeListener( new ChangeListener(){
            	@Override
            	public void stateChanged(ChangeEvent event){
            		sliderLabel.setText("Volume: "+slider.getValue()+" %");
            	}
            }
            );
            Box sliderbox = Box.createVerticalBox();

            sliderbox.add(sliderLabel, sliderbox.CENTER_ALIGNMENT);
            sliderbox.add(slider);
            contentPane.add(sliderbox);
            slider.putClientProperty("Nimbus.Overrides",sliderDefaults);

            
            contentPane.add(previousButton);
            contentPane.add(playButton);
            contentPane.add(nextButton);
            contentPane.add(libraryButton);
            contentPane.setBounds(0, 0,
                    this.getWidth(),
                    this.getHeight());
	        contentPane.setOpaque(false);
			add(contentPane,BorderLayout.NORTH);
			cardlayout.show(cards, "content");
			
        }
        public void createFrame(int Content){
        	//desktop.createFrame(desktop.getSize(), Content);
        }
        public void lock(Boolean b){
        	//desktop.lock(b);
        }

        public float getSliderVal(float min, float max){
        	//System.out.print(min + " ");
        	//System.out.println(max);
        	if(slider.getValue() == 0){
        		return min;
        	}
        	if(min < - 30){
        		min = -30F;
        	}
        	if(max > 30){
        		max = 30F;
        	}
        	System.out.println((slider.getValue()));
        	return (float) (  min +  ((slider.getValue() * (Math.abs(min) + Math.abs(max)) )/100) );
        }
        
        public void songStopped(){
        	playing = false;
        	playButton.setIcon(img0);
            playButton.setRolloverIcon(rollover0);
            playButton.setActionCommand("play");
        }
        
        @SuppressWarnings("deprecation")
        public void actionPerformed(ActionEvent arg0) {
            if(arg0.getActionCommand().equals("play")){
            	if (newPlayer != null) {
                    newPlayer.stop();
                }
                newPlayer = new DecodeAndPlayAudio();
                songFileName = musicSelector.selectedFile();
                newPlayer.args = songFileName;
                DecodeAndPlayAudio.volume = 0F;
                DecodeAndPlayAudio.ISinstance = this;
                newPlayer.start();
                playing = true;
                paused = false;
                System.out.println("playing song");
                playButton.setIcon(img1);
                playButton.setRolloverIcon(rollover1);
                playButton.setActionCommand("pause");
                //set song information
                //currentArtist = "The Police";
                if (songFileName.equals("The Police - Every Breath You Take.mp3")) {
                    currentSong = "Every Breath You Take";
                    currentAlbum = "Synchronicity";
                } else {
                    currentSong = "Message In A Bottle";
                    currentAlbum = "Reggatta de Blanc";
                }
                //artist.setText(currentArtist);
                //song.setText(currentSong);
                InternetCheck ic = new InternetCheck();
                //if(ic.reach == true) desktop.updateContent(currentArtist,currentSong,currentAlbum);
                //else artist.setText("NO INTERNET");

            }
            if(arg0.getActionCommand().equals("pause")){
                //pause
            	if(playing){
            		if(paused){
            			playButton.setIcon(img1);
                        playButton.setRolloverIcon(rollover1);
            		newPlayer.resume();
                	System.out.println("resuming song");
                	paused = false;
            		}
            		else{ 
            			playButton.setIcon(img0);
                        playButton.setRolloverIcon(rollover0);
                        playButton.setActionCommand("pause");
            			newPlayer.suspend();
                    	System.out.println("pausing song");
                    	paused = true;
            		}
            	}
            }
            
            if(arg0.getActionCommand().equals("library")){
                //show the library
            	cardlayout.show(cards, select);
            	
            	if(select=="music")select="content";
            	else select="music";
            	
            }
            if(arg0.getActionCommand().equals("stop")){
                //stop
            	newPlayer.stop();
            	paused = false;
            	playing = false;
                currentSong = "Click play to start selected song.";
                currentArtist = "No song playing.";
                artist.setText(currentArtist);
                song.setText(currentSong);
                contentSelector.clearContent();
                //albumArt.setIcon(null);*/
                System.out.println("stopping song");
            }
        }
        
}